package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.service.INursingProjectService;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        boolean result = save(nursingProject);
        if (result){
            redisTemplate.delete(CacheConstants.CACHE_KEY_NURSING_PROJECT);
        }
        return result ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        boolean result = updateById(nursingProject);
        if (result){
            redisTemplate.delete(CacheConstants.CACHE_KEY_NURSING_PROJECT);
        }
        return result ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        boolean result = removeByIds(Arrays.asList(ids));
        if (result){
            redisTemplate.delete(CacheConstants.CACHE_KEY_NURSING_PROJECT);
        }
        return result ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        boolean result = removeById(id);
        if (result){
            redisTemplate.delete(CacheConstants.CACHE_KEY_NURSING_PROJECT);
        }
        return result ? 1 : 0;
    }

    /**
     * 获取所有启用护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {
        //加入缓存机制，先从缓存中获取
        List<NursingProjectVo> voList = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CacheConstants.CACHE_KEY_NURSING_PROJECT);
        // 缓存中存在则返回
        if (voList != null){
            return voList;
        }
        // 缓存中不存在则从数据库中查询,并保存到缓存中
        //1.查询所有启用护理项目
        List<NursingProject> list = lambdaQuery().eq(NursingProject::getStatus, 1)
                .select(NursingProject::getId, NursingProject::getName)
                .list();
        //2.封装成list返回
        List<NursingProjectVo> voListDB = list.stream().map(p ->
//             NursingProjectVo.builder()
//                    .label(p.getName())
//                    .value(String.valueOf(p.getId()))
//                    .build()
                        NursingProjectVo.of(p.getName(), String.valueOf(p.getId()))
        ).collect(Collectors.toList());
        //方式二：
//        List<NursingProjectVo> voList1 = this.getBaseMapper().selectNursingProjectVoList();
        // 保存到缓存中
        redisTemplate.opsForValue().set(CacheConstants.CACHE_KEY_NURSING_PROJECT, voListDB);
        return voListDB;
    }
}
